package controller

import (
	"fmt"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// GetPostList
// @Tags SysPost
// @Summary 查询部门列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.QueryPost true " "
// @success 200 {object} response.Response{data=[]model.SysPost} "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /post/getPostList [get]
func GetPostList(c *gin.Context) {
	var pageInfo request.QueryPost
	_ = c.ShouldBindQuery(&pageInfo)
	fmt.Printf("%+v", pageInfo)

	if err := utils.Verify(pageInfo); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := service.GetPostInfoList(pageInfo)
	pagination := response.Pagination{PageNum: pageInfo.PageNum, PageSize: pageInfo.PageSize, Total: total}
	response.RespondList(list, pagination, err, c)
}

// AddPost
// @Tags SysPost
// @Summary 新增部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysPost true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /post/addPost [post]
func CreatePost(c *gin.Context) {
	var post model.SysPost
	_ = c.ShouldBindJSON(&post)
	if err := utils.Verify(post); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	post.CreateTime = time.Now()
	post.UpdateTime = time.Now()
	post.CreateBy = user.Username
	post.UpdateBy = user.Username

	err := service.CreatePost(post)
	response.RespondOperate(err, c)
}

// UpdatePost
// @Tags SysPost
// @Summary 更新部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysPost true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /post/updatePost [put]
func UpdatePost(c *gin.Context) {
	var post model.SysPost
	_ = c.ShouldBindJSON(&post)
	if err := utils.Verify(post); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	post.UpdateTime = time.Now()
	post.UpdateBy = user.Username

	err := service.UpdatePost(post)
	response.RespondOperate(err, c)
}

// QueryPost
// @Tags SysPost
// @Summary 查询部门信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /post/{id} [get]
func QueryPost(c *gin.Context) {
	postId, _ := strconv.Atoi(c.Param("id"))
	if postId == 0 {
		response.FailWithMessage("id不能为空", c)
	}

	post, err := service.QueryPost(uint64(postId))
	response.RespondData(post, err, c)
}

// DeletePost
// @Tags SysPost
// @Summary 批量删除
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /post/deletePost [delete]
func DeletePost(c *gin.Context) {
	var reqIds request.IdsReq
	_ = c.ShouldBindJSON(&reqIds)
	if err := utils.Verify(reqIds); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := service.DeletePost(reqIds.Ids)
	response.RespondOperate(err, c)
}
